package server

import (
	"github.com/gin-gonic/gin"
	"github.com/starmotioncf/common/cfgstruct"
	"github.com/starmotioncf/common/server/http"
	"github.com/starmotioncf/soutchatinternal/middleware"
	router2 "github.com/starmotioncf/soutchatinternal/router"
)

var registerRouters = make([]func(r gin.IRouter), 0)

func NewHTTPServer(
	conf http.Config,
	withoutLoginRouter *router2.WithoutLoginRouter,
	mustLoginRouter *router2.MustLoginRouter,
	openapiRouter *router2.OpenapiRouter,
	thirdCallbackRouter *router2.ThirdCallbackRouter,
) *http.Server {
	switch cfgstruct.DefaultsType() {
	case cfgstruct.DefaultsRelease:
		gin.SetMode(gin.ReleaseMode)
	case cfgstruct.DefaultsTest:
		gin.SetMode(gin.TestMode)
	default:
		gin.SetMode(gin.DebugMode)
	}
	s := http.NewServer(gin.New(), conf)

	//外部可选编译handler
	for i := range registerRouters {
		registerRouters[i](s)
	}

	// 添加公共中间件
	// 添加请求日志中间件
	s.Use(gin.Recovery(), middleware.RequestLogMiddleware())
	//内部handler
	// 第三方回调
	thirdCallbackRouter.Register(s)
	// 无需登陆
	withoutLoginRouter.Register(s)
	// 必须登陆
	mustLoginRouter.Register(s)
	// openapi
	openapiRouter.Register(s)
	return s
}
